package com.op.studyingliteapp.activity.sonseminar;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.op.studyingliteapp.R;
import com.op.studyingliteapp.common.Common;
import com.op.studyingliteapp.common.SeminarsUrl;
import com.op.util.ToastUtils;
import com.op.util.asynctask.HttpPostTask;
import com.op.util.asynctask.OnTaskProcessListener;

public class sonSeminarAdapter extends BaseAdapter{
	private static final String TAG =  sonSeminarAdapter.class.getSimpleName();
	private Context mContext;
	private ArrayList<sonSeminarData> sonSeminarList;
	private SharedPreferences preferences;
	private String getuserId, getSeminarId;
	public sonSeminarAdapter(Context mContext,ArrayList<sonSeminarData> sonSeminarList) {
		this.mContext = mContext;
		this.sonSeminarList=sonSeminarList;
	}
	public Context getContext(){
		return mContext;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return sonSeminarList.size();
	}

	@Override
	public sonSeminarData getItem(int position) {
		// TODO Auto-generated method stub
		return sonSeminarList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		
		final DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(null)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
		
		LinearLayout LL= null;
		if(convertView!=null){
			LL=(LinearLayout)convertView;
		}else{
			LL=(LinearLayout)LayoutInflater.from(getContext()).inflate(R.layout.item_son_seminar,null);
		}

		final sonSeminarData data=getItem(position);
		
		ImageView headPortrait = (ImageView) LL.findViewById(R.id.headPortrait);
		TextView name =(TextView) LL.findViewById(R.id.name);
		TextView time = (TextView) LL.findViewById(R.id.time);
		TextView context = (TextView) LL.findViewById(R.id.context);
		TextView title = (TextView) LL.findViewById(R.id.title);
		/**
		 * 从网上下拉的图片添加头像里面
		 */
		ImageLoader.getInstance().displayImage(data.getHeadPortrait(), headPortrait, options);
		name.setText(data.getName());
		time.setText(data.getTime());
		context.setText(data.getWord());
		title.setText(data.getTitle());
		
		preferences = mContext.getSharedPreferences("StudyingLite", Context.MODE_PRIVATE);
		getuserId = preferences.getString(Common.Prefs.USER_ID, "");
		
		return LL;
	}
	
	/**
	 * 删除网上数据库帖子方法
	 */
	public void deleteItem(String SeminarId, String Userid) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("seminarId", SeminarId));
		params.add(new BasicNameValuePair("userId", Userid));
		Log.i(TAG, "SeminarId:"+SeminarId+"UserId"+Userid);
		HttpPostTask task = new HttpPostTask(mContext, SeminarsUrl.DELETE_SEMINARS_BY_SEMINAR_ID_AND_USER_ID, params);
		task.setTaskListener(new OnTaskProcessListener() {

			@Override
			public void prepare() {

			}

			@Override
			public void completed(String result) {
				
			}
		});
		notifyDataSetChanged();
		task.execute();
	}
	

}
